<?php

if( isset($_POST['action']) && $_POST['action'] == "addposmenu" )
{
    if( !isset($_SESSION['basket']) )
        $_SESSION['basket'] = array('suma');    
        
    if( !isset($_SESSION['basket'][$_GET['restauracja']]) )
        $_SESSION['basket'][$_GET['restauracja']] = array();    
        
    if( !isset($_SESSION['basket'][$_GET['restauracja']][$_POST['pos']]) )
        $_SESSION['basket'][$_GET['restauracja']][$_POST['pos']] = 0;

    $_SESSION['basket'][$_GET['restauracja']][$_POST['pos']]++;
}
elseif( isset($_POST['action']) && $_POST['action'] == "delposmenu" )
{
    $_SESSION['basket'][$_GET['restauracja']][$_POST['pos']]--;
}

$suma = 0;
?>

<div id="res1" class="col-sm-10">
    <table class="table table-striped">
        <?php
        
            $query = $db->query( 'SELECT *, menupositions.id as mid FROM menupositions INNER JOIN restaurants ON restaurant = restaurants.id WHERE rname = "'.$_GET['restauracja'].'"' );
                
            if( $query->rowCount() == 0 )
            {
                echo '<div class="msg_error">Nie jesteś w zasięgu żadnej restauracji! ;( </div>';
            }
                
            $i = 1;
            foreach ($query as $row)
            {
                $ilosc = ( isset( $_SESSION['basket'][$_GET['restauracja']][$row['mid']] ) )  ? $_SESSION['basket'][$_GET['restauracja']][$row['mid']] : 0;
            
                $suma += $ilosc * $row['price'];
                
                echo '<tr id="'.($i++).'">
                        <td>
                            <h4 class="list-group-item-heading">'.$row['name'].'</h4>
                            <p class="list-group-item-text">'.$row['description'].'</p>
                        </td>
                        <td>
                            <span class="badge middlePrice">'.number_format($row['price']/100, 2, '.', '').' zł</span>
                        </td>
                        <td style="font-size: 14px; font-weight: bold; text-align: center; vertical-align: middle;">'.$ilosc.'</td>
                        <td>
                            <form action="?restauracja='.$_GET['restauracja'].'" method="POST" style="display: inline-block">
                                <input type="hidden" name="action" value="addposmenu"/>
                                <input type="hidden" name="pos" value="'.$row['mid'].'"/>
                                <button type="submit" value="" style="width: 40x; background: none; border: none">
                                    <img src="img/orders/add_to_shopping_cart.png" class="shoppingCartButton">
                                 </button>
                            </form>';       
                            
                     if( $ilosc > 0)       
                        echo '<form action="?restauracja='.$_GET['restauracja'].'" method="POST" style="display: inline-block">
                            <input type="hidden" name="action" value="delposmenu"/>
                            <input type="hidden" name="pos" value="'.$row['mid'].'"/>
                            <button type="submit" value="" style="width: 40x; background: none; border: none">
                                <img src="img/orders/delete_basket.png" class="shoppingCartButton">
                                </button>
                        </form>';
                
                 echo '</td>
                    </tr>';
            }
            
            $_SESSION['basket']['suma'] = $suma;
        ?>
    </table>
    <hr />
    <h3>Suma zamówienia <span id="priceSum" class="label label-default"><?php echo number_format($suma/100, 2, '.', ''); ?> zł</span></h3>
    <?php
        $disabled = ( !isset($_SESSION['user']) ) ? 'disabled = "disabled"' : "";
    ?>
    <form action="?podsumowanie" method="POST">
        <input type="hidden" name="rest" value="<?php echo $_GET['restauracja']; ?>"/>
        <input id="sendOrder" type="submit" class="btn btn-success" value="Wyślij zamówienie!" <?php echo $disabled; ?>/>
    </form>
    <?php
        if(!empty($disabled))
        {
            echo '<br/><br/><div class="msg_error">Zaloguj się, aby złożyć zamówienie!</div>';
        }
    ?>
</div>